<?php


namespace app\logic;


use think\facade\Db;

class GoodsHelper
{
    public static function expand_goods_category_ids($goods_category_ids) {
        $goods_category_ids_new = [];
        $goods_category_titles_new = [];

        $goods_category_ids = explode(',', $goods_category_ids);
        foreach ($goods_category_ids as $goods_category_id) {
            if ($goods_category_id > 0) {
                $goods_category = DataHelper::find_by_id('tb_goods_category', $goods_category_id);

                $goods_category_ids_new[] = $goods_category_id;
                $goods_category_titles_new[] = $goods_category['title'];

                if ($goods_category['pid'] > 0) {
                    $goods_category_ids_new[] = $goods_category['pid'];
                    $goods_category_titles_new[] = $goods_category['pname'];
                }
            }
        }

        $goods_category_ids_new = array_unique($goods_category_ids_new);
        $goods_category_titles_new = array_unique($goods_category_titles_new);

        return [$goods_category_ids_new, $goods_category_titles_new];
    }

    public static function load_goods_category($pid = 0, $checked_ids = []) {


        $list = DataHelper::select('tb_goods_category', [
            'pid' => $pid,
            'status' => 0
        ]);

        foreach ($list as &$item) {
            if (in_array($item['id'], $checked_ids)) {
                $item['checked'] = true;
            }
        }

        foreach ($list as &$item) {
            $children = self::load_goods_category($item['id']);
            $item['children'] = $children;
        }

        return $list;
    }

    public static function update_goods_spec($goods_id) {
        $goods = DataHelper::find_by_id('tb_goods', $goods_id);
        $spec_name_list = [];

        $spec1_value = explode(',', $goods['spec1_value']);
        $spec2_value = explode(',', $goods['spec2_value']);
        $spec3_value = explode(',', $goods['spec3_value']);

        foreach ($spec1_value as $spec1) {
            foreach ($spec2_value as $spec2) {
                foreach ($spec3_value as $spec3) {

                    $spec_name = '';
                    if ($goods['spec1_status'] == 1) $spec_name .= $spec1.",";
                    if ($goods['spec2_status'] == 1) $spec_name .= $spec2.",";
                    if ($goods['spec3_status'] == 1) $spec_name .= $spec3.",";
                    $spec_name = trim($spec_name, ',');

                    if (!in_array($spec_name, $spec_name_list)) {
                        $spec_name_list[] = $spec_name;
                    }
                }
            }
        }


        DataHelper::update('tb_goods_spec', [
            [ 'goods_id', '=', $goods_id ],
            [ 'spec_name', 'not in', $spec_name_list ]
        ], [ 'status' => -1 ]);

        foreach ($spec_name_list as $spec_name) {

            $tmp = DataHelper::find('tb_goods_spec', [
                [ 'goods_id', '=', $goods_id ],
                [ 'spec_name', '=', $spec_name ]
            ]);

            if (empty($tmp)) {
                DataHelper::add('tb_goods_spec', [
                    'goods_id' => $goods_id,
                    'spec_name' => $spec_name
                ]);
            }
        }
    }
}